home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / getext.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  1KB  |  64 lines

  1. /***********************************************************************
  2.  * $Id: getext.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *
  8.  *   return filename extension
  9.  ***********************************************************************/
  10.  
  11. #if !defined(lint) && defined(F_ID)
  12. char *id_gext = "$Id: getext.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  13. #endif
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "ulib.h"
  18.  
  19. const char *
  20. file_ext(const char *f)
  21. {
  22.     const char *q;
  23.  
  24.     if (!f || !*f)
  25.     return 0;
  26.     q = f + strlen(f) - 1;
  27.     while (q > f && *q != '.' && *q != '/')
  28.     q--;
  29.     return *q == '.' ? q + 1 : 0;
  30. }
  31.  
  32. /******************************************************************
  33.  * given a complete filename, return the root part, similar to what
  34.  * :t does in csh
  35.  ******************************************************************/
  36.  
  37. #define MAXBUF 3
  38. const char *
  39. file_tail(const char *f)
  40. {
  41.     static char fbuf[MAXBUF][PATH_MAX];
  42.     static int cbuf;
  43.     char *p, *buf;
  44.  
  45.     buf = fbuf[cbuf++];
  46.  
  47.     strcpy(buf, f);
  48.  
  49.     /* remove extension first */
  50.     if ((p = strchr(buf, '.')))
  51.       {
  52.       *p = '\0';
  53.       p = buf;
  54.       }
  55.  
  56.     /* get tail */
  57.     if ((p = strrchr(buf, '/')))
  58.     *p++ = '\0';
  59.  
  60.     cbuf %= MAXBUF;
  61.  
  62.     return p ? p : buf;
  63. }
  64.